// pages/my/wallet/index.js
import {
    _headerPost,
    _headerGet
} from '../../../../utils/httpUtils.js'
const RSA = require("../../../../utils/wx_rsa.js");
const app = getApp();
Page({

    data: {
        navbarData: {
            showCapsule: 1, //是否显示左上角图标
            title: '余额提现' //导航栏 中间的标题
        },
        height: app.globalData.height * 2 + 20,
        putMoney: '',
        keyBoard: true,
        pwdPay: '',
        keyNum: ['1', '4', '7', '2', '5', '8', '3', '6', '9'],
        bankCard: ''
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
        this.getCard()
    },
    //获取银行卡
    getCard() {
        _headerGet('//api/user/getUserMoney', {}, res => {
            this.setData({
                bankCard: res.data.data.default_card,
                balance: res.data.data.balance_value
            })
        })
    },
    //输入金                         
    inputMoney(e) {
        this.setData({
            putMoney: e.detail.value
        })
    },
    //全部提现
    allPutForward() {
        this.setData({
            putMoney: this.data.balance
        })
    },
    //选择银行卡
    selBank() {
        wx.navigateTo({
            url: '../../../public/bank/bank-card/index?Type=selete',
        })
    },
    select(e) {
        this.setData({
            selectItem: e.currentTarget.id
        })
    },
    //提现
    putMoney() {
        let data = {
            wo_mold: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(`user`)),
            bc_id: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(`${this.data.bankCard.ubc_id}`)), //银行卡ID
            wo_money: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(`${this.data.putMoney}`)), //提取金额
            c_client: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(`weixinSmall`))
        }
        _headerPost('/api/Wallet/withdraw', data, res => {
            if (res.data.code == 1)
                wx.showModal({
                    title: '提示',
                    content: '申请提现已提交',
                    showCancel: false,
                    success: res => {
                        wx.navigateBack({})
                    }
                })
            else {
                wx.showModal({
                    title: '提示',
                    content: res.data.msg,
                    showCancel: false
                })
            }
        })
    },
    //显示键盘
    hiddKeyBoard() {
        if (this.data.putMoney < 100) {
            wx.showModal({
                title: '提示',
                content: '提现金额最少100元',
                showCancel: false
            })
            return
        }

        this.putMoney(); //直接提现目前不需要密码
        return;
        if (this.data.bankCard == null) {
            wx.showModal({
                title: '提示',
                content: '请选择银行卡',
                showCancel: false
            })
            return
        }
        this.setData({
            keyBoard: false
        })
    },
    //隐藏键盘
    nohiddKeyBoard() {
        this.setData({
            keyBoard: true
        })
    },
    //键盘输入事件
    numClick(e) {
        if (e.currentTarget.id == 'cls') {
            let num = this.data.pwdPay
            this.setData({
                pwdPay: num.substring(0, num.length - 1)
            })
        } else if (e.currentTarget.id == 'del') {
            this.setData({
                pwdPay: ''
            })
        } else if (this.data.pwdPay.length < 6) {
            this.setData({
                pwdPay: this.data.pwdPay + e.currentTarget.id
            })
        }
        if (this.data.pwdPay.length == 6) {
            _headerPost('/index/User/checkpaypwd', {
                password: this.data.pwdPay
            }, res => {
                if (res.data.code == 1) {
                    this.nohiddKeyBoard()
                    this.putMoney()
                } else {
                    wx.showModal({
                        title: '提示',
                        content: res.data.msg,
                        showCancel: false
                    })
                }
            }, "", comp => {
                this.setData({
                    pwdPay: ''
                })
            })
        }
    },
    close() {
        // 取消冒泡事件
    }
})